package cn.iocoder.yudao.module.reading.convert.record;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.reading.controller.app.record.vo.*;
import cn.iocoder.yudao.module.reading.dal.dataobject.record.ReadingRecordDO;
import cn.iocoder.yudao.module.reading.dal.dataobject.book.PictureBookDO;
import cn.iocoder.yudao.module.reading.dal.dataobject.bookset.BookSetDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 阅读记录 Convert
 *
 * @author 李晨
 */
@Mapper
public interface ReadingRecordConvert {

    ReadingRecordConvert INSTANCE = Mappers.getMapper(ReadingRecordConvert.class);

    /**
     * 转换为阅读记录历史响应 VO
     *
     * @param recordDO 阅读记录DO
     * @param bookDO   绘本DO
     * @param bookSetDO 绘本集DO
     * @return 阅读记录历史响应 VO
     */
    default ReadingHistoryRespVO convertToHistoryRespVO(ReadingRecordDO recordDO, PictureBookDO bookDO, BookSetDO bookSetDO) {
        ReadingHistoryRespVO vo = BeanUtils.toBean(recordDO, ReadingHistoryRespVO.class);

        if (bookDO != null) {
            vo.setBookTitle(bookDO.getTitle());
            vo.setBookCover(bookDO.getCover());
            vo.setBookSetId(bookDO.getBookSetId());
        }

        if (bookSetDO != null) {
            vo.setBookSetTitle(bookSetDO.getTitle());
            vo.setBookLanguage(bookSetDO.getLanguage());
        }

        return vo;
    }

    /**
     * 转换为今日阅读状态响应 VO
     *
     * @param todayRecords 今日阅读记录列表
     * @param todayReadingTime 今日阅读时长
     * @return 今日阅读状态响应 VO
     */
    default ReadingStatusRespVO convertToStatusRespVO(List<ReadingRecordDO> todayRecords, Integer todayReadingTime) {
        ReadingStatusRespVO vo = new ReadingStatusRespVO();
        
        vo.setHasReadToday(!todayRecords.isEmpty());
        vo.setTodayBookCount(todayRecords.size());
        vo.setTodayReadingTime(todayReadingTime != null ? todayReadingTime / 60 : 0); // 转换为分钟
        vo.setCanPunch(vo.getHasReadToday() && vo.getTodayBookCount() >= 1);
        
        return vo;
    }

    /**
     * 转换为阅读统计响应 VO
     *
     * @param totalBooks 总阅读绘本数
     * @param todayBooks 今日阅读绘本数
     * @param weekBooks 本周阅读绘本数
     * @param monthBooks 本月阅读绘本数
     * @param totalReadingTime 总阅读时长
     * @param todayReadingTime 今日阅读时长
     * @param consecutiveDays 连续阅读天数
     * @param maxConsecutiveDays 最长连续阅读天数
     * @param monthlyStats 月度统计
     * @return 阅读统计响应 VO
     */
    default ReadingStatsRespVO convertToStatsRespVO(Integer totalBooks, Integer todayBooks, 
                                                   Integer weekBooks, Integer monthBooks,
                                                   Integer totalReadingTime, Integer todayReadingTime,
                                                   Integer consecutiveDays, Integer maxConsecutiveDays,
                                                   List<ReadingStatsRespVO.MonthlyStatsVO> monthlyStats) {
        ReadingStatsRespVO vo = new ReadingStatsRespVO();
        
        vo.setTotalBooks(totalBooks != null ? totalBooks : 0);
        vo.setTodayBooks(todayBooks != null ? todayBooks : 0);
        vo.setWeekBooks(weekBooks != null ? weekBooks : 0);
        vo.setMonthBooks(monthBooks != null ? monthBooks : 0);
        
        vo.setTotalReadingTime(totalReadingTime != null ? totalReadingTime / 60 : 0); // 转换为分钟
        vo.setTodayReadingTime(todayReadingTime != null ? todayReadingTime / 60 : 0); // 转换为分钟
        
        // 计算平均阅读时长
        if (totalBooks != null && totalBooks > 0 && totalReadingTime != null) {
            vo.setAverageReadingTime(totalReadingTime / 60 / totalBooks);
        } else {
            vo.setAverageReadingTime(0);
        }
        
        vo.setConsecutiveDays(consecutiveDays != null ? consecutiveDays : 0);
        vo.setMaxConsecutiveDays(maxConsecutiveDays != null ? maxConsecutiveDays : 0);
        vo.setMonthlyStats(monthlyStats);
        
        return vo;
    }

    /**
     * 转换为月度统计VO
     *
     * @param monthlyData 月度数据Map，key为月份，value为统计数据
     * @return 月度统计VO列表
     */
    default List<ReadingStatsRespVO.MonthlyStatsVO> convertToMonthlyStats(Map<String, Map<String, Integer>> monthlyData) {
        return monthlyData.entrySet().stream()
                .map(entry -> {
                    ReadingStatsRespVO.MonthlyStatsVO monthlyVO = new ReadingStatsRespVO.MonthlyStatsVO();
                    monthlyVO.setMonth(entry.getKey());
                    
                    Map<String, Integer> data = entry.getValue();
                    monthlyVO.setBookCount(data.getOrDefault("bookCount", 0));
                    monthlyVO.setReadingTime(data.getOrDefault("readingTime", 0) / 60); // 转换为分钟
                    monthlyVO.setReadingDays(data.getOrDefault("readingDays", 0));
                    
                    return monthlyVO;
                })
                .collect(Collectors.toList());
    }

}
